class PagesController < ApplicationController
    
    layout 'default'
    
    # GET /pages
    # GET /pages.xml
    def index
        @pages = Page.find(:all)
        
        respond_to do |format|
            format.html # index.rhtml
            format.xml  { render :xml => @pages.to_xml }
        end
    end
    
    # GET /pages/1
    # GET /pages/1.xml
    def show
        @page = Page.find(params[:id])
        @annorations = @page.annorations
        @title = @annorations[0].title
        @uri = @annorations[0].uri
        
        respond_to do |format|
            format.html # show.rhtml
            format.xml  { render :xml => @page.to_xml }
        end
    end
    
    # GET /pages/new
    def new
        @page = Page.new
    end
    
    # GET /pages/1;edit
    def edit
        @page = Page.find(params[:id])
    end
    
    # POST /pages
    # POST /pages.xml
    def create
        @page = Page.new(params[:page])
        
        respond_to do |format|
            if @page.save
                flash[:notice] = 'Page was successfully created.'
                format.html { redirect_to page_url(@page) }
                format.xml  { head :created, :location => page_url(@page) }
            else
                format.html { render :action => "new" }
                format.xml  { render :xml => @page.errors.to_xml }
            end
        end
    end
    
    # PUT /pages/1
    # PUT /pages/1.xml
    def update
        @page = Page.find(params[:id])
        
        respond_to do |format|
            if @page.update_attributes(params[:page])
                flash[:notice] = 'Page was successfully updated.'
                format.html { redirect_to page_url(@page) }
                format.xml  { head :ok }
            else
                format.html { render :action => "edit" }
                format.xml  { render :xml => @page.errors.to_xml }
            end
        end
    end
    
    # DELETE /pages/1
    # DELETE /pages/1.xml
    def destroy
        @page = Page.find(params[:id])
        @page.destroy
        
        respond_to do |format|
            format.html { redirect_to pages_url }
            format.xml  { head :ok }
        end
    end
    
    #####################################
    # Used by Annosquat and html snippets.
    def widget_annotations
        uri = params[:u]
        #&showStars=true&showNumberRatings=true&showTopRatings=true&numberTopRatings=5
        @showHeader = params[:showHeader]
        @showStars = params[:showStars]
        @requireLogin = params[:requireLogin]
        @showNumberRatings = params[:showNumberRatings]
        @numberTopRatings = params[:numberTopRatings]
        @averageRatingTitle = params[:averageRatingTitle].nil? ? "" : params[:averageRatingTitle]
        @showAverageRatings = params[:showAverageRatings]
        
        @page = Page.find_by_uri(uri)
        if @numberTopRatings && @page
            @annorations = @page.annorations.find(:all, :limit => @numberTopRatings.to_i, :order => "created_at DESC")
        end
        render :partial => "widget_annotation"        
    end
end
